package com.test.service.Impl;

import com.test.Aspect.BusinessException;
import com.test.dao.OrderMapper;
import com.test.domain.Order;
import com.test.domain.OrderExample;
import com.test.enums.Page;
import com.test.enums.Replay;
import com.test.enums.ReplayCode;
import com.test.service.OrderService;
import com.test.utils.ReplayUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * OrderServiceImpl
 *
 * @author HY
 * 2021/05/24 8:44
 **/


@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderMapper orderMapper;

    @Override
    public Replay insert(Order order) {
        //业务处理，判断关键参数是否为空
        //判断商品id是否为空
        if (order.getStoreid() == null || order.getStoreid().equals("")){
            throw new BusinessException(ReplayCode.REQUIREDNULL_ERR);
        }
        //判断用户id是否为空
        if (order.getUserid() == null || order.getUserid().equals("")){
            throw new BusinessException(ReplayCode.REQUIREDNULL_ERR);
        }

        int rs = orderMapper.insert(order);

        if (rs < 1 ){
            throw new BusinessException(ReplayCode.DO_ERR);
        }
        return ReplayUtil.success();
    }

    @Override
    public Replay del(int id) {
        // TODO 删除待完成
        return ReplayUtil.success();
    }

    @Override
    public Replay update(Order order) {

        if (order.getOrderid() == null || order.getOrderid().equals("")){
            throw new BusinessException(101,"必填参数不能为空");
        }

        int rs = orderMapper.updateByPrimaryKeySelective(order);

        if (rs < 1){
            throw new BusinessException(ReplayCode.DO_ERR);
        }

        return ReplayUtil.success();
    }

    @Override
    public Replay select(Order order, Page page) {

        if (page.isNull()){
            //抛出空值异常
            throw new BusinessException(ReplayCode.Page_NULL);
        }

        OrderExample orderExample = new OrderExample();
        OrderExample.Criteria criteria = orderExample.createCriteria();

        //查询条件

        List<Order> orders = orderMapper.selectByExample(orderExample);
        int count = (int) orderMapper.countByExample(orderExample);

        return ReplayUtil.success("查询成功", orders, count);
    }
}
